package com.job.elasticjob;

import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import com.job.TestDao;

import java.util.Map;

/**
 * ElasticJobLite定时任务业务逻辑处理类
 */
public class ArchivieJob implements SimpleJob {


    private TestDao testDao = new TestDao();

    /**
     * 需求：resume表中未归档的数据归档到resume_bak表中，每次归档1条记录
     * execute方法中写我们的业务逻辑（execute方法每次定时任务执行都会执行一次）
     *
     * @param shardingContext
     */
    @Override
    public void execute(ShardingContext shardingContext) {
        int shardingItem = shardingContext.getShardingItem();
        System.out.println("=====>>>>当前分片：" + shardingItem);

        // 获取分片参数 // 0=bachelor,1=master,2=doctor
        String shardingParameter = shardingContext.getShardingParameter();

        // 1 从resume表中查询出1条记录（未归档）
        Map<String, Object> stringObjectMap = testDao.getData(shardingParameter);
        if (stringObjectMap == null) {
            System.out.println("数据已经处理完毕！！！！！！");
            return;
        }
        // 2 "未归档"更改为"已归档"
        long id = 0;
        String name = null;
        String education = null;
        try {
            id = (long) stringObjectMap.get("id");
            name = (String) stringObjectMap.get("name");
            education = (String) stringObjectMap.get("education");
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("=======>>>>id：" + id + "  name：" + name + " education：" + education);

        testDao.update(id);

        // 3 归档这条记录，把这条记录插入到resume_bak表
        testDao.backData(id);
    }
}
